<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('入库单据')"/>
    <style>
        [v-cloak] {
            display: none;
        }
        .ul_ {
            position: absolute;
            right: 0;
            width: 200px;
            height: 200px;
            z-index: 999999;
            background-color: #fff;
            overflow: auto;
            border: 1px solid #dddddd;
        }
        .ul_::-webkit-scrollbar {
            display: none;
        }
        .ul_ li {
            cursor: pointer;
            width: 100%;
            text-indent: 5px;
            margin: 0;
        }
        .ul_ li:hover {
            background: #c8c8c9;
        }
        .el-date-editor.el-input, .el-date-editor.el-input__inner {
            width: 200px!important;
        }
        .el-input__icon {
            line-height: 0!important;
        }
        .tag {
            display: inline-block;
            padding: 0 5px;
            line-height: 22px;
            color: #FFFFFF;
            border-radius: 3px;
        }
        .el-tag.el-tag--info {
            color: #000!important;
        }
    </style>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li v-if="checkedSearch.indexOf('入库单号') != -1">
                            入库单号：<input type="text" id="billNo" name="billNo"/>
                        </li>
                        <li v-if="checkedSearch.indexOf('入库类型') != -1">
                            入库类型：
                            <select name="type">
                                <option value="">全部</option>
                                <option value="1">采购入库</option>
                                <option value="2">生产退料</option>
<!--                                <option value="3">采购预收</option>-->
                                <option value="4">试做品入库</option>
                                <option value="5">采购预收</option>
                            </select>
                        </li>
                        <li v-if="checkedSearch.indexOf('单据状态') != -1">
                            单据状态：
                            <select name="state">
                                <option value="">全部</option>
                                <option value="1">待入库</option>
                                <option value="2">部分入库</option>
                                <option value="3">入库完成</option>
                                <option value="4">过账完成</option>
                                <option value="5">过账上传中</option>
                                <option value="6">过账失败</option>
                            </select>
                        </li>
                        <li  v-if="checkedSearch.indexOf('供应商名称') != -1" style="position: relative">
                            供应商名称：
                            <select name="supplierName" placeholder="请选择制单人">
                                <option v-for="(item, index) in suppliers" :key="index" :label="item.supplierName" :value="item.supplierName">{{item.supplierName}}</option>
                            </select>
                        </li>
                        <li v-if="checkedSearch.indexOf('创建人') != -1">
                            创建人：<input type="text" id="createUserName" name="createUserName"/>
                        </li>
                        <li v-show="checkedSearch.indexOf('创建时间') != -1">
                            <label>创建时间：</label>
                            <el-date-picker
                                name="startTime"
                                v-model="timeA"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                type="datetime"
                                @change="compareAB"
                                placeholder="开始时间">
                            </el-date-picker>
                            <span>-</span>
                            <el-date-picker
                                name="endTime"
                                v-model="timeB"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                type="datetime"
                                @change="compareAB"
                                placeholder="结束时间">
                            </el-date-picker>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()" @click="myReset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            <a class="btn btn-success btn-rounded btn-sm" @click="moreSearch()"><i class="fa fa-edit"></i>&nbsp;更多</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <el-dialog title="搜索条件" width="200px" :visible.sync="showMore">
            <el-row>
                <el-col :span="24">
                    <el-checkbox style="margin-left: 28px" :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
                    <el-checkbox-group v-model="checkedSearch" @change="handleCheckedSearchChange">
                        <el-checkbox style="display: block;margin-left: 28px" v-for="item in searches" :label="item" :key="item" v-cloak>{{item}}</el-checkbox>
                    </el-checkbox-group>
                </el-col>
            </el-row>
        </el-dialog>
        <div class="btn-group-sm" id="toolbar" role="group">
<!--            <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="in:billInMaster:export">-->
<!--                <i class="fa fa-download"></i> 导出全部-->
<!--            </a>-->
<!--            <a class="btn btn-info" @click="partExcel()" shiro:hasPermission="in:billInMaster:partExcel">-->
<!--                <i class="fa fa-download"></i> 部分导出-->
<!--            </a>-->
            <a class="btn btn-primary" onclick="$.table.print()">
                <i class="fa fa-plus"></i> 打印
            </a>
            <a class="btn btn-success" @click="add1">
                <i class="fa fa-plus"></i> 新增
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table" data-mobile-responsive="true" style="white-space: nowrap"></table>
        </div>
    </div>
</div>

<div th:include="include :: footer"></div>
<script th:src="@{/wms/js/moreSearch.js}"></script>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('in:billInMaster:edit')}]];
    var prefix = ctx + "in/billInMaster";

    const searchOptions = ['入库单号', '入库类型', '单据状态', '供应商名称', '创建人', '创建时间'];

    var app = new Vue({
        el: '.container-div',
        data: {
            showMore: false,
            checkAll: false,
            checkedSearch: ['单据状态', '创建人', '创建时间', '供应商名称'],
            searches: searchOptions,
            isIndeterminate: true,
            suppliers: [],
            supplierName: '',
            inputSupplierId: '',
            showSupplierUl: false,
            supplierList: [],
            timeA: '',
            timeB: '',
            users: [],
            checkCreateUserList: [],
            allCheckCreateUser: false,
            checkCreateUserName: ''
        },
        methods: {
            add1() {
                var url = prefix + '/add'
                $.modal.open("新增", url, '1000', '600');
            },
            //函数防抖
            supplier_debounce(wait){
                clearTimeout(this.timer);
                this.timer = setTimeout(() => {
                    this.getSelectSupplierData(this.supplierName.trim());
                }, wait)
            },
            //已选供应商
            chooseSupplier(val, id){
                this.supplierName = val;
                this.inputSupplierId = id;
            },
            //重置输入供应商
            supplierReset(){
                this.supplierName = '';
                this.inputSupplierId = '';
            },
            //验证输入供应商
            supplierInputYZ(){
                if(this.supplierName.trim() != ''){
                    var arr = [];
                    for(var i = 0; i < this.suppliers.length; i++){
                        arr.push(this.suppliers[i].supplierName);
                    }
                    var index = $.inArray(this.supplierName.trim(), arr);
                    if(index < 0){
                        $.modal.alertError("不存在该供应商");
                        this.supplierReset();
                    }
                }else{
                    this.supplierReset();
                }
            },
            //供应商输入框聚焦
            focusSupplier(){
                if(document.getElementsByClassName('supplierInput')[0] == document.activeElement){
                    this.supplier_debounce(500);
                }
                this.showSupplierUl = true;
            },
            //获取供应商列表
            getSuppliers: function(){
                var that = this;
                $.ajax({
                    cache: true,
                    type: "POST",
                    url: "/system/supplier/list",
                    data: {},
                    async: false,
                    error: function(request){
                        $.modal.alertError("系统错误");
                    },
                    success: function(data){
                        that.suppliers = data.rows;
                    }
                });
            },
            //时间对比
            compareAB: function(){
                if(this.timeA != '' && this.timeA != null){
                    if(this.timeB != '' && this.timeB != null){
                        var a = new Date(this.timeA);
                        var b = new Date(this.timeB);
                        if(a >= b){
                            $.modal.alertError("结束时间需大于开始时间，请重新选择！！");
                            this.timeA = '';
                            this.timeB = '';
                        }
                    }else{
                        this.timeB = '';
                    }
                }else{
                    this.timeA = '';
                    if(this.timeB == null){
                        this.timeB = '';
                    }
                }
            },
            //清空时间
            resetTime(){
                this.timeA = '';
                this.timeB = '';
            },
            //获取制单人列表
            getUsers(){
                var that = this;
                $.ajax({
                    cache: true,
                    type: "POST",
                    url: "/system/user/list",
                    data: {},
                    async: false,
                    error: function(request){
                        $.modal.alertError("系统错误");
                    },
                    success: function(data){
                        that.users = data.rows;
                    }
                });
            },
            //制单人下拉框全选
            selectAll(){
                this.checkCreateUserList = []
                if(this.allCheckCreateUser){
                    var tmpArr = [];
                    this.users.forEach((item) => {
                        tmpArr.push(item.userName);
                    })
                    this.checkCreateUserList = tmpArr;
                    this.checkCreateUserName = this.checkCreateUserList.toString();
                }else{
                    this.checkCreateUserList = [];
                    this.checkCreateUserName = '';
                }
            },
            //切换制单人全选状态
            changeSelect(val){
                if(val.length == this.users.length) {
                    this.allCheckCreateUser = true;
                }else{
                    this.allCheckCreateUser = false;
                }
                this.checkCreateUserName = this.checkCreateUserList.toString();
            },
            //清空制单人
            resetCheckCreateUserName(){
                this.checkCreateUserList = [];
                this.checkCreateUserName = '';
            },
            //重置按钮
            myReset(){
                this.supplierReset();
                this.resetCheckCreateUserName();
                this.resetTime();
            },
            //获取前n天的日期
            getBeforeDate(n){
                var d = new Date();
                var year = d.getFullYear();
                var mon = d.getMonth() + 1;
                var day = d.getDate();
                if(day <= n){
                    if(mon > 1){
                        mon = mon - 1;
                    }else{
                        year = year - 1;
                        mon = 12;
                    }
                }
                d.setDate(d.getDate() - n);
                year = d.getFullYear();
                mon = d.getMonth() + 1;
                day = d.getDate();
                var s = year + '-' + (mon < 10 ? ('0' + mon) : mon) + '-' + (day < 10 ? ('0' + day) : day);
                return s;
            },
            // 批量删除
            delAll() {
                var rows = $.common.isEmpty($.table._option.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns($.table._option.uniqueId);
                if(rows.length == 0){
                    $.modal.alertWarning("请至少选择一条记录");
                    return;
                }else{
                    $.modal.confirm("确认要删除选中的" + rows.length + "条数据吗?", function(){
                        // var url = prefix + "/partExcel";
                        // var data = {"ids": rows.join()};
                        // $.post(url, data, function(result){
                        //     if(result.code == web_status.SUCCESS){
                        //         window.location.href = ctx + "common/download?fileName=" + encodeURI(result.msg) + "&delete=" + true;
                        //     }else if(result.code == web_status.WARNING){
                        //         $.modal.alertWarning(result.msg);
                        //     }else{
                        //         $.modal.alertError(result.msg);
                        //     }
                        //     $.modal.closeLoading();
                        // });
                    });
                }
            },
            //部分导出
            partExcel(){
                var rows = $.common.isEmpty($.table._option.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns($.table._option.uniqueId);
                if(rows.length == 0){
                    $.modal.alertWarning("请至少选择一条记录");
                    return;
                }else{
                    $.modal.confirm("确认要导出选中的" + rows.length + "条数据吗?", function(){
                        var url = prefix + "/partExcel";
                        var data = {"ids": rows.join()};
                        $.post(url, data, function(result){
                            if(result.code == web_status.SUCCESS){
                                window.location.href = ctx + "common/download?fileName=" + encodeURI(result.msg) + "&delete=" + true;
                            }else if(result.code == web_status.WARNING){
                                $.modal.alertWarning(result.msg);
                            }else{
                                $.modal.alertError(result.msg);
                            }
                            $.modal.closeLoading();
                        });
                    });
                }
            }
        },
        created(){
            this.getSuppliers();
            this.getUsers();
            this.timeA = this.getBeforeDate(30) + ' 00:00:00';
            this.timeB = this.getBeforeDate(0) + ' 23:59:59';
        },
        watch: {
            'supplierName': {
                handler: function(){
                    this.focusSupplier();
                }
            }
        },
        mounted(){
            let that = this;
            document.addEventListener('click', function(e){
                if(e.target.className != 'supplierInput'){
                    that.$nextTick(() => {
                        that.showSupplierUl = false;
                    })
                }
            })
        }
    })

    $(function () {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            exportUrl: prefix + "/export",
            updateUrl: prefix + "/edit/{id}",
            detailUrl: prefix + "/detail",
            modalName: "入库单据查询",
            queryParams: queryParams,
            columns: [
                {
                    checkbox: true
                },
                {
                    field: 'billId',
                    title: '序号',
                    align: 'center',
                    visible: false
                },
                {
                    field: 'billNo',
                    title: '入库单号',
                    align: 'center'
                },
                {
                    field: 'sapNo',
                    title: '外部交货单号',
                    align: 'center'
                },
                {
                    field: 'sapState',
                    title: 'SPA过账状态',
                    align: 'center',
                    formatter: function(value, row, index){ 
                        var actions = [];
                        if(value == '1'){
                            actions.push("<span class='tag' style='background-color: #999999'>未过账</span>");
                        }else if(value == '2'){
                            actions.push("<span class='tag' style='background-color: #f8ac59'>已过账</span>");
                        }else if(value == '3'){
                            actions.push("<span class='tag' style='background-color: #1ab394'>部分过账</span>");
                        }else{
                            actions.push("<span></span>");
                        }
                        return actions.join('');
                    }
                },
                {
                    field: 'type',
                    title: '入库类型',
                    align: 'center',
                    formatter: function(value, row, index){
                        var actions = [];
                        if(value == '1'){
                            actions.push("<span class='tag' style='background-color: #fcb529'>采购入库</span>");
                        }else if(value == '2'){
                            actions.push("<span class='tag' style='background-color: #45ade1'>生产退料</span>");
                        }else if(value == '4'){
                            actions.push("<span class='tag' style='background-color: #1ab394'>试做品</span>");
                        }else if(value == '5'){
                            actions.push("<span class='tag' style='background-color: #1ab394'>采购预收</span>");
                        }
                        return actions.join('');
                    }
                },
                {
                    field: 'state',
                    title: '单据状态',
                    align: 'center',
                    formatter: function(value, row, index){
                        var actions = [];
                        if(value == 1 || value == null){
                            actions.push("<span class='tag' style='background-color: #999999'>待入库</span>");
                        }else if(value == 2){
                            actions.push("<span class='tag' style='background-color: #f8ac59'>部分入库</span>");
                        }else if(value == 3){
                            actions.push("<span class='tag' style='background-color: #1ab394'>入库完成</span>");
                        }else if(value == 4){
                            actions.push("<span class='tag' style='background-color: #45ade1'>过账完成</span>");
                        }else if(value == 5){
                            actions.push("<span class='tag' style='background-color: #fcb529'>过账上传中</span>");
                        }else if(value == 6){
                            actions.push("<span class='tag' style='background-color: #f8ac59'>过账失败</span>");
                        }
                        return actions.join('');
                    }
                },
                {
                    field: 'supplierName',
                    title: '供应商名称',
                    align: 'center'
                },
                {
                    field: 'createUserName',
                    title: '创建人',
                    align: 'center',
                    formatter: function(value, item, index){
                        if(value == null){
                            return '';
                        }else{
                            return value;
                        }
                    }
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    align: 'center',
                    formatter: function(value, item, index){
                        if(value == null){
                            return '';
                        }else{
                            return value.slice(0, 19);
                        }
                    }
                },
                {
                    field: 'auditUserName',
                    title: '审核人',
                    align: 'center',
                    formatter: function(value, item, index){
                        if(value == null){
                            return '';
                        }else{
                            return value;
                        }
                    }
                },
                {
                    field: 'auditTime',
                    title: '审核时间',
                    align: 'center',
                    formatter: function(value, item, index){
                        if(value == null){
                            return '';
                        }else{
                            return value.slice(0, 19);
                        }
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    events: {
                        'click .detail': function(e, value, rowData, index){
                            var id = rowData.billId;
                            var _url = $.table._option.detailUrl;
                            localStorage.setItem(_url, id.toString());
                            $.modal.openFull("入库单据明细", _url);
                        },
                        'click .delect': function(e, value, rowData, index){
                            var id = rowData.billId;
                            $.modal.confirm("确认要删除选中的数据吗?", function(){
                                // var url = prefix + "/partExcel";
                                // var data = {"ids": rows.join()};
                                // $.post(url, data, function(result){
                                //     if(result.code == web_status.SUCCESS){
                                //         window.location.href = ctx + "common/download?fileName=" + encodeURI(result.msg) + "&delete=" + true;
                                //     }else if(result.code == web_status.WARNING){
                                //         $.modal.alertWarning(result.msg);
                                //     }else{
                                //         $.modal.alertError(result.msg);
                                //     }
                                //     $.modal.closeLoading();
                                // });
                            });
                        },
                        'click .update': function(e, value, rowData, index){
                            var id = rowData.billId;
                            var _url = $.table._option.updateUrl;
                            localStorage.setItem(_url, id.toString());
                            $.modal.openFull("入库单据修改", _url);
                        },
                    },
                    formatter: function(value, row, index){
                        var actions = [];
                        if (row.type == 3 && row.state == 1) {
                            actions.push('<span class="delect tag" style="background-color: #ed5565;cursor: pointer"><i class="fa fa-remove"></i> 删除</span> ');
                            actions.push('<span class="update tag" style="background-color: #1c84c6;cursor: pointer"><i class="fa fa-edit"></i> 修改</span> ');
                        }
                        actions.push("<span class='detail tag' style='background-color: #3097b5;cursor: pointer'><i class='fa fa-hand-pointer-o'></i> 查看</span>");
                        return actions.join('');
                    }
                }
            ]
        };
        $.table.init(options);
    });

    function queryParams(params){
        return {
            pageSize: params.limit,
            pageNum: params.offset / params.limit + 1,
            orderByColumn: params.sort,
            isAsc: params.order,
            startTime: app.timeA,
            endTime: app.timeB
        };
    }
</script>
</body>
</html>
